package net.microfigure.ljfshiro.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.microfigure.ljfshiro.mapper.UserMapper;
import net.microfigure.ljfshiro.mapper.UserRoleMapper;
import net.microfigure.ljfshiro.model.User;
import net.microfigure.ljfshiro.model.UserRole;
import net.microfigure.ljfshiro.service.UserService;
import net.microfigure.ljfshiro.util.ResultUtil;
import net.microfigure.ljfshiro.vo.base.ResponseVo;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;

	@Override
	public User selectByUsername(String username) {
		// TODO Auto-generated method stub
		return userMapper.selectByUsername(username);
	}

	@Override
	public int register(User user) {
		// TODO Auto-generated method stub
		int a = userMapper.insert(user);
		return a;
	}

	@Override
	public void updateLastLoginTime(User user) {
		userMapper.updateLastLoginTime(user);

	}



	@Override
	public User selectByUserId(String userId) {
		// TODO Auto-generated method stub
		return userMapper.selectByUserId(userId);
	}

	@Override
	public int updateByUserId(User user) {
		// TODO Auto-generated method stub
		return userMapper.updateByUserId(user);
	}

	@Override
	public int updateStatusBatch(List<String> userIds, Integer status) {
		Map<String,Object> params = new HashMap<String,Object>(2);
		params.put("userIds",userIds);
		params.put("status",status);
		return userMapper.updateStatusBatch(params);
	}

	@Override
	public ResponseVo addAssignRole(String userId, List<String> roleIds) {
		// TODO Auto-generated method stub
		//根据用户id分配角色集合
		try{
			UserRole userRole = new UserRole();
			userRole.setUserId(userId);
			userRoleMapper.delete(userRole);
			for(String roleId : roleIds){
				userRole.setId(null);
				userRole.setRoleId(roleId);
				userRoleMapper.insert(userRole);
			}
			return ResultUtil.success("分配角色成功");
		}catch(Exception e){
			return ResultUtil.error("分配角色失败");
		}
	}

	@Override
	public int updateUserByPrimaryKey(User user) {
		// TODO Auto-generated method stub
		 return userMapper.updateByPrimaryKey(user);
	}

	@Override
	public List<User> selectUsers(User user) {
		// TODO Auto-generated method stub
		return userMapper.selectUsers(user);
	}

}
